﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace _4_7._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void LoadBackGround()
        {
            try
            {
                // 尝试加载背景图片
                if (System.IO.File.Exists("发射场.jpg"))
                {
                    Bitmap backPic = new Bitmap("发射场.jpg");
                    this.BackgroundImage = backPic;
                    this.ClientSize = new Size(backPic.Width, backPic.Height);
                }
                else
                {
                    CreateBackgroundPlaceholder();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载背景图片失败: {ex.Message}");
                CreateBackgroundPlaceholder();
            }
        }

        private void CreateBackgroundPlaceholder()
        {
            // 创建背景替代图片
            Bitmap placeholder = new Bitmap(800, 600);
            using (Graphics g = Graphics.FromImage(placeholder))
            {
                g.Clear(Color.DarkBlue);

                // 绘制星空
                Random rand = new Random();
                for (int i = 0; i < 100; i++)
                {
                    int x = rand.Next(800);
                    int y = rand.Next(600);
                    g.FillEllipse(Brushes.White, x, y, 2, 2);
                }

                // 绘制地面
                g.FillRectangle(new SolidBrush(Color.DarkGreen), 0, 500, 800, 100);

                // 绘制文字
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                g.DrawString("火箭发射场\n\n请将'发射场.jpg'图片文件\n放置在应用程序目录中",
                            new Font("Arial", 14, FontStyle.Bold),
                            Brushes.White,
                            new RectangleF(0, 0, 800, 500),
                            format);
            }
            this.BackgroundImage = placeholder;
            this.ClientSize = new Size(800, 600);
        }

        private void InitRocket()
        {
            try
            {
                if (System.IO.File.Exists("火箭.gif"))
                {
                    Bitmap pic = new Bitmap("火箭.gif");
                    pictureBox1.Image = pic;
                    pictureBox1.BackColor = Color.Transparent;
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    pictureBox1.Size = new Size(pic.Width / 2, pic.Height / 2); // 适当缩放

                    // 设置初始位置：水平居中，底部隐藏在窗体下方
                    pictureBox1.Left = this.ClientSize.Width / 2 - pictureBox1.Width / 2;
                    pictureBox1.Top = this.ClientSize.Height - pictureBox1.Height + 100;
                }
                else
                {
                    CreateRocketPlaceholder();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载火箭图片失败: {ex.Message}");
                CreateRocketPlaceholder();
            }
        }

        private void CreateRocketPlaceholder()
        {
            // 创建火箭替代图片
            Bitmap placeholder = new Bitmap(50, 100);
            using (Graphics g = Graphics.FromImage(placeholder))
            {
                g.Clear(Color.Transparent);

                // 绘制火箭形状
                Point[] rocketBody = {
                    new Point(25, 0),
                    new Point(40, 40),
                    new Point(40, 80),
                    new Point(10, 80),
                    new Point(10, 40)
                };

                g.FillPolygon(Brushes.Silver, rocketBody);
                g.DrawPolygon(Pens.White, rocketBody);

                // 绘制火箭尾部火焰
                Point[] flame = {
                    new Point(15, 80),
                    new Point(25, 100),
                    new Point(35, 80)
                };

                g.FillPolygon(Brushes.OrangeRed, flame);
            }

            pictureBox1.Image = placeholder;
            pictureBox1.BackColor = Color.Transparent;
            pictureBox1.Size = new Size(50, 100);
            pictureBox1.Left = this.ClientSize.Width / 2 - pictureBox1.Width / 2;
            pictureBox1.Top = this.ClientSize.Height - pictureBox1.Height + 100;
        }

        private void InitProgress()
        {
            progressBar1.Dock = DockStyle.Top;
            // 进度条最大值 = 火箭从初始位置到完全飞出窗体的距离
            progressBar1.Maximum = pictureBox1.Top + pictureBox1.Height;
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            progressBar1.Height = 20;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 初始化界面
            LoadBackGround();
            InitRocket();
            InitProgress();

            // 设置定时器
            timer1.Interval = 1; // 1ms 间隔，确保流畅动画
            timer1.Enabled = false;

            // 设置窗体标题和双击提示
            this.Text = "火箭发射程序 - 双击窗体开始发射";
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                // 开始发射
                timer1.Enabled = true;
                this.Text = "火箭发射中...";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 火箭上升逻辑
            if (pictureBox1.Top + pictureBox1.Height > 0)
            {
                pictureBox1.Top -= 1; // 火箭上升
                progressBar1.Value += 1; // 进度条前进
            }
            else
            {
                // 火箭已飞出窗体
                timer1.Enabled = false;
                this.Text = "火箭发射完成！";
                MessageBox.Show("火箭发射成功！已进入预定轨道。", "发射完成",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);

                // 可选：重置发射状态
                // ResetLaunch();
            }
        }

        // 重置发射状态的方法（可选）
        private void ResetLaunch()
        {
            timer1.Enabled = false;
            InitRocket(); // 重新初始化火箭位置
            progressBar1.Value = 0;
            this.Text = "火箭发射程序 - 双击窗体开始发射";
        }

        // 添加键盘控制（空格键发射）
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space && !timer1.Enabled)
            {
                timer1.Enabled = true;
                this.Text = "火箭发射中...";
            }
        }

        // 添加右键菜单重置功能
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenuStrip contextMenu = new ContextMenuStrip();
                ToolStripMenuItem resetItem = new ToolStripMenuItem("重置发射");
                resetItem.Click += (s, args) => ResetLaunch();
                contextMenu.Items.Add(resetItem);

                contextMenu.Show(this, e.Location);
            }
        }
    }
}
